package org.fastdfs.test.fastdfsye.utils;

import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.Set;

@Component
public class UploadUtils {
    public String upload(byte[] bytes, String filr_exe_name, Map<String,String> map) throws IOException, MyException {
        try {
            ClientGlobal.initByProperties("fastdfs-client.properties");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (MyException e) {
            e.printStackTrace();
        }
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer track = trackerClient.getConnection();

        StorageServer storageServer=null;
        StorageClient1 client1=new StorageClient1(track,storageServer);
        NameValuePair[] nameValuePairs = new NameValuePair[map.size()];
        Set<String> keys = map.keySet();
        int index=0;
        for (String key:keys){
            String s = map.get(key);
            nameValuePairs[index]=new NameValuePair(key,s);
            index++;
        }
        String url = client1.upload_file1(bytes, "jpeg", nameValuePairs);
        track.close();
        return url;
    }
    public void download(String url) throws IOException, MyException {
        ClientGlobal.initByProperties("fastdfs-client.properties");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer track = trackerClient.getConnection();

        StorageServer storageServer=null;
        StorageClient1 client1=new StorageClient1(track,storageServer);
        byte[] bytes = client1.download_file1(url);
        File file = new File("/Users/liquan/note/a.jpg");
        FileOutputStream outputStream = new FileOutputStream(file);
        outputStream.write(bytes);
        track.close();

    }
    public void delete(String url) throws IOException, MyException {
        ClientGlobal.initByProperties("fastdfs-client.properties");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer track = trackerClient.getConnection();

        StorageServer storageServer=null;
        StorageClient1 client1=new StorageClient1(track,storageServer);
        int i = client1.delete_file1(url);
        if(i>=0){
            System.out.println("删除成功!");
        }
        System.out.println(i);
        track.close();
    }
    public void FindFile(String url) throws IOException, MyException {
        ClientGlobal.initByProperties("fastdfs-client.properties");
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer track = trackerClient.getConnection();

        StorageServer storageServer=null;
        StorageClient1 client1=new StorageClient1(track,storageServer);
        FileInfo fileInfo = client1.query_file_info1(url);
        System.out.println(fileInfo);
        NameValuePair[] metadata1 = client1.get_metadata1(url);
        System.out.println(metadata1);
    }
}
